import KUi from '@kernel/util/ui'
import UI from '@data/ui'
import KCamera from '@kernel/util/camera'
import EventUtil from '@kernel/util/event_util'
import KPlayer from '@kernel/util/player'
import KPlayerGroup from '@kernel/util/player_group'
import HeroSelectController from './HeroSelectController'
import InitAttack from '../enemy/AttackController'

// 难度选择
const DifficultyLevelSelect = () => {
  // ui - 难度选择
  EventUtil.register('TRIGGER_COMPONENT_EVENT', (data) => {
    if (data.__ui_event_name !== 'DifficultyLevelSelect') {
      return
    }
    const player = KPlayer.fromId(data.__role_id)
    const ui = new KUi(<string>data.__comp_name, player)
    const temps = ui.name.split('.')
    const level = Number(temps[1] ?? 0)

    const list = KPlayerGroup.fromPlaying()
    list.forEach(target => {
      const currentDifficultyLevel = KUi.searchPath(KPlayer.fromId(1), UI.CurrentDifficultyLevel)
      currentDifficultyLevel.setText('难度' + level)
      // 打开英雄选择框
      HeroSelectController.fromPlayer(target)
    })
    // 隐藏难度选择框
    KUi.searchPath(KPlayer.fromId(1), UI.DifficultyLevel).visible(false)
    InitAttack()
  })
}

// ui初始化
const InitUI = () => {
  EventUtil.register('GAME_INIT', () => {
    const player = KPlayer.fromLocal()
    // 隐藏默认ui
    if (player.id != 1) {
      KUi.searchPath(player, UI.DifficultyLevel).visible(false)
    }
    KUi.searchPath(player, UI.HeroSelect).visible(false)
  })

  DifficultyLevelSelect()
}

export default InitUI
